﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Xml;
using Yu.Cms.Common;
using System.IO;

namespace Yu.Cms.Web.UserCtrl.Admin
{
    public partial class RotatePic : BaseCtrl
    {
        //private string configPath = Config.AggregationFileManager.ConfigFilePath;
        private string configPath = Utils.GetMapPath(Yu.Cms.Config.BaseConfigs.GetSitePath + "config/aggregation.config");
        private string nodeName = "RotatePic";
        private string targetNode = "/AggregationInfo/Website_RotatePiclist";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                 
                this.OnStar();                
            }
        }
        protected void OnStar()
        {
            XmlDataSource1.DataFile = configPath;
            this.BindPic();
        }

        protected void BindPic()
        {
           
            XmlDataSource1.XPath = "/AggregationInfo/Website_RotatePiclist/RotatePic";
            RptPic.DataSource = XmlDataSource1;
            RptPic.DataBind();
        }

        /// <summary>
        /// 添加聚合图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if ((!Utils.IsURL(txtImg.Text.Trim()) || (!Utils.IsURL(txtUrl.Text.Trim()))))
            {
                JScript.AlertAndGoHistory("图片路径或点击链接可能是非法URL");
                return;
            }
            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(configPath);
            int lastRotatepicid = 0;
            if (doc.SelectSingleNode(targetNode) != null)
            {
                if (doc.SelectSingleNode(targetNode).InnerText != "")
                {
                    lastRotatepicid = int.Parse(doc.SelectSingleNode(targetNode).LastChild["PicID"].InnerText);
                }
            }

            lastRotatepicid++;
            XmlElement rotatepicNode = doc.CreateElement(nodeName);
            doc.AppendChildElementByNameValue(ref rotatepicNode, "PicID", lastRotatepicid.ToString());
            doc.AppendChildElementByNameValue(ref rotatepicNode, "Img", txtImg.Text.Trim());
            doc.AppendChildElementByNameValue(ref rotatepicNode, "Url", txtUrl.Text.Trim());
            doc.AppendChildElementByNameValue(ref rotatepicNode, "Title", txtTitle.Text.Trim());

            doc.CreateNode(targetNode).AppendChild(rotatepicNode);
            doc.Save(configPath);
            //BindPic();
            JScript.AlertAndRedirect("添加成功", "RotatePic.aspx");
        }

        /// <summary>
        /// 修改所选中的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnModify_Click(object sender, EventArgs e)
        {
            #region 保存轮换图片修改
           
            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(configPath);
            XmlNodeList __xmlnodelist = doc.SelectSingleNode(targetNode).ChildNodes;

            if ((__xmlnodelist != null) && (__xmlnodelist.Count > 0))
            {
                doc.InitializeNode(targetNode);
            }

            string PicID = string.Empty;
            string Img = string.Empty;
            string Url = string.Empty;
            string Title = string.Empty;
            
            foreach (RepeaterItem Item in RptPic.Items)
            {
                CheckBox chk = (CheckBox)Item.FindControl("chkIndex");
                //if (chk.Checked)
                //{
                    TextBox txtID = (TextBox)Item.FindControl("txtPicID");
                    PicID = txtID.Text.Trim();
                    TextBox txtImg = (TextBox)Item.FindControl("txtImg");
                    Img = txtImg.Text.Trim();
                    TextBox txtUrl = (TextBox)Item.FindControl("txtUrl");
                    Url = txtUrl.Text.Trim();
                    TextBox txtTitle = (TextBox)Item.FindControl("txtTitle");
                    Title = txtTitle.Text.Trim();
                    if (!Utils.IsNumeric(PicID) || !Utils.IsURL(Img) || !Utils.IsURL(Url) || string.IsNullOrEmpty(Title))
                    {
                        JScript.AlertAndGoHistory("某行序号、图片路径或点击链接可能是非法URL或说明文字为空，不能进行更新.");
                        return;
                    }
                    bool insert = false;
                    XmlElement rotatepicNode = doc.CreateElement(nodeName);
                    doc.AppendChildElementByNameValue(ref rotatepicNode, "PicID", PicID);
                    doc.AppendChildElementByNameValue(ref rotatepicNode, "Img", Img);
                    doc.AppendChildElementByNameValue(ref rotatepicNode, "Url", Url);
                    doc.AppendChildElementByNameValue(ref rotatepicNode, "Title", Title);
                    foreach (XmlNode __node in __xmlnodelist)
                    {
                        if (int.Parse(__node["PicID"].InnerText) > int.Parse(PicID))
                        {
                            doc.SelectSingleNode(targetNode).InsertBefore(rotatepicNode, __node);
                            insert = true;
                            break;
                        }
                    }
                    if (!insert)
                    {
                        doc.SelectSingleNode(targetNode).AppendChild(rotatepicNode);                       
                    }
               // }
            }
            doc.Save(configPath);
            //更新文件的最后修改时间
            FileInfo fi = new FileInfo(configPath);
            fi.LastWriteTime = DateTime.Now;
            JScript.AlertAndRedirect("修改成功", "RotatePic.aspx");
            #endregion
        }

        /// <summary>
        /// 删除所选中的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            string PicID = string.Empty;
            foreach (RepeaterItem Item in RptPic.Items)
            {
                TextBox txtID = (TextBox)Item.FindControl("txtPicID");
                CheckBox chk = (CheckBox)Item.FindControl("chkIndex");                
                if (chk.Checked)
                {
                    PicID += txtID.Text + ",";
                }
            }
            PicID = PicID.TrimEnd(',');
            if (PicID != "")
            {
                int delcount = 1;
                XmlDocumentExtender doc = new XmlDocumentExtender();
                doc.Load(configPath);
                XmlNodeList __xmlnodelist = doc.SelectSingleNode(targetNode).ChildNodes;
                foreach (string menuid in PicID.Split(','))
                {
                    doc.SelectSingleNode(targetNode).RemoveChild(__xmlnodelist.Item(int.Parse(menuid) - delcount));
                    delcount++;
                }
                doc.Save(configPath);               
                JScript.AlertAndRedirect("删除成功", "RotatePic.aspx");
            }
            else
            {
                JScript.AlertAndGoHistory("未选择任何项目");    
            }
            
        }

       
    }
}